package cc.ycws.api.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cc.ycws.core.dao.INSERTUserMapper;
import cc.ycws.core.domain.ango;
import com.alibaba.fastjson.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;


@Component
public class TaskCool {

    @Resource
    INSERTUserMapper iNSERTUserMapper;

    /**
     * 定时器调度任务
     */
    public void testJob() {
        String n = "1";
        String urlStr = "http://120.77.173.150:8070/user/match/upload";//安全GO接口地址
        List<ango> listango = iNSERTUserMapper.selectangodrawandblank();
        ;//查询存入的数据
        if (listango.size() == 0) {
            System.out.println("没有上传失败的数据");
        } else {
            String jsonone = drawandblank(listango, urlStr);//安全GO接口
            if (jsonone != null) {
                String statemen2 = JSONObject.parseObject(jsonone).getString("status");
                if (statemen2.equals(n)) {//成功更新状态
                    for (int i = 0; i < listango.size(); i++) {
                        int in = listango.get(i).getId();
                        String id = String.valueOf(in);
                        String statemen = n;
                        iNSERTUserMapper.Updateuserstateandto(statemen, id);
                    }
                } else {
                    System.out.println("上传安全GO失败");
                }
            } else {
                System.out.println("上传安全GO失败");
            }
        }

    }

    /**
     * httpclient post请求安全GO接口
     *
     * @param listango
     * @param urlStr
     * @return
     */
    public String drawandblank(List<ango> listango, String urlStr) {
        new JSONObject();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("listone", listango);
        JSONObject json = new JSONObject(map);
        String jsonstr = json.toString();
        String result = null;
        HttpPost httpPost = new HttpPost(urlStr);
        try {
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            // httpPost.setHeader("Content-Type", "application/json");
            StringEntity se = new StringEntity(jsonstr, "UTF-8");
            se.setContentType("text/json");
            httpPost.setEntity(se);
            @SuppressWarnings("resource")
            HttpClient httpClient = new DefaultHttpClient();// 获取HttpClient对象
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);// 连接超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);// 请求超时

            HttpResponse httpResp = httpClient.execute(httpPost);// 获取HttpResponse实例
            if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
                result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据
            } else {
                return result;
            }
        } catch (ConnectTimeoutException e) {
            return result;
        } catch (UnsupportedEncodingException e) {
            return result;
        } catch (IOException e) {
            return result;
        } catch (Exception e) {
            return result;
        }

        return result;
    }

}



